package flca.mda.api.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import mda.type.IEntityType;
import flca.mda.codegen.helpers.StrUtil;

public class DojoTypeUtils extends WebappTypeUtils {
//	private NameUtils nu = new NameUtils();
	private WebappIntferfaceUtils iu = new WebappIntferfaceUtils();

	// private TemplateUtils tplu = new TemplateUtils();

	/**
	 * return a constant like CUSTOMER_RETRIEVE_SUCCESS given a Class Customer
	 * and an Enum ITEM_RETRIEVE_SUCCESS The const values are maintained in
	 * dojo.ini
	 * 
	 * @param aClass
	 * @param aConstantType
	 * @return
	 */
	public String getConstant(Class<?> aClass, ConstType aConstantType) {
		Properties props = new Properties();
		props.put("entity_const", aClass.getSimpleName().toUpperCase());
		String result = StrUtil.subsProperties(constTypesMap.get(aConstantType), props);
		if (result == null) {
			result = "?" + aConstantType + "?";
		}
		return result;
	}

	/**
	 * This is a enhanced version of the original
	 * ebappInterfaceUtils.getSubsWithsForCrudOper method. This methods add an
	 * entru for all ConstantsTypes's.
	 * 
	 * @param aClass
	 * @return
	 */
	public Properties getSubsWithsForDojoCrudOper(CrudMethod aCrudMethod) {
		Properties result = iu.getSubsWithsForCrudOper(aCrudMethod, false);

		Class<?> crudent = aCrudMethod.getEntity();

		for (ConstType ctype : ConstType.values()) {
			result.put(ctype.name(), getConstant(crudent, ctype));
		}
		return result;
	}

	public static enum ConstType {
		ITEM_RETRIEVE, ITEM_RETRIEVE_SUCCESS, ITEM_RETRIEVE_ERROR, ITEM_SAVE, ITEM_SAVE_SUCCESS, ITEM_SAVE_ERROR, 
		ITEM_FINDALL, ITEM_FINDALL_SUCCESS, ITEM_FINDALL_ERROR, ITEM_SEARCH, ITEM_SEARCH_SUCCESS, ITEM_SEARCH_ERROR, 
		ITEM_REMOVE, ITEM_REMOVE_SUCCESS, ITEM_REMOVE_ERROR, 
		ADD_ITEM, EDIT_ITEM, SHOW_ITEM_GRID, SHOW_ITEM_DETAIL, 
		REMOVE_ITEM_GRID, CLOSE_ITEM_DETAIL, REMOVE_ITEM_DETAIL,
		DIV_ITEM_DETAIL_ID,
	}

	private static final Map<ConstType, String> constTypesMap = new HashMap<DojoTypeUtils.ConstType, String>() {
		private static final long serialVersionUID = 3220274762342928563L;
		{
			put(ConstType.ADD_ITEM, "<%=entity_const%>_ADD");
			put(ConstType.EDIT_ITEM, "<%=entity_const%>_EDIT");
			put(ConstType.ITEM_RETRIEVE, "<%=entity_const%>_RETRIEVE");
			put(ConstType.ITEM_RETRIEVE_SUCCESS, "<%=entity_const%>_RETRIEVE_SUCCESS");
			put(ConstType.ITEM_RETRIEVE_ERROR, "<%=entity_const%>_RETRIEVE_ERROR");
			put(ConstType.ITEM_SAVE, "<%=entity_const%>_SAVE");
			put(ConstType.ITEM_SAVE_SUCCESS, "<%=entity_const%>_SAVE_SUCCESS");
			put(ConstType.ITEM_SAVE_ERROR, "<%=entity_const%>_SAVE_ERROR");
			put(ConstType.ITEM_FINDALL, "<%=entity_const%>_FINDALL");
			put(ConstType.ITEM_FINDALL_SUCCESS, "<%=entity_const%>_FINDALL_SUCCESS");
			put(ConstType.ITEM_FINDALL_ERROR, "<%=entity_const%>_FINDALL_ERROR");
			put(ConstType.ITEM_SEARCH, "<%=entity_const%>_SEARCH");
			put(ConstType.ITEM_SEARCH_SUCCESS, "<%=entity_const%>_SEARCH_SUCCESS");
			put(ConstType.ITEM_SEARCH_ERROR, "<%=entity_const%>_SEARCH_ERROR");
			put(ConstType.ITEM_REMOVE, "<%=entity_const%>_REMOVE");
			put(ConstType.ITEM_REMOVE_SUCCESS, "<%=entity_const%>_REMOVE_SUCCESS");
			put(ConstType.ITEM_REMOVE_ERROR, "<%=entity_const%>_REMOVE_ERROR");
			put(ConstType.SHOW_ITEM_GRID, "<%=entity_const%>_SHOW_GRID");
			put(ConstType.SHOW_ITEM_DETAIL, "<%=entity_const%>_SHOW_DETAIL");
			put(ConstType.REMOVE_ITEM_GRID, "<%=entity_const%>_REMOVE_GRID");
			put(ConstType.CLOSE_ITEM_DETAIL, "<%=entity_const%>_CLOSE_DETAIL");
			put(ConstType.REMOVE_ITEM_DETAIL, "<%=entity_const%>_REMOVE_DETAIL");
			put(ConstType.DIV_ITEM_DETAIL_ID, "DIV_<%=entity_const%>_DETAIL_ID");
		}
	};

	/**
	 * return all dojo constants for all entities, with generated line like
	 * A_RETRIEVE : "A_RETRIEVE",
	 * 
	 * @return
	 */
	public String getAllDojoConstants() {
		StringBuffer sb = new StringBuffer();
		List<Class<?>> entities = ModelClassesUtils.findModelClassesWithInterface(IEntityType.class);
		for (Class<?> e : entities) {
			for (String strval : constTypesMap.values()) {
				String s = StrUtil.replace(strval, "<%=entity_const%>", e.getSimpleName().toUpperCase());
				sb.append("\t\t" + s + " : \"" + s.toLowerCase() + "\",\n");
			}
		}
		return sb.toString();
	}
}
